package com.zhonghui.mes.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.zhonghui.common.utils.SecurityUtils;
import com.zhonghui.mes.domain.vo.MesProductInspectorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhonghui.mes.mapper.MesProductInspectorMapper;
import com.zhonghui.mes.domain.MesProductInspector;
import com.zhonghui.mes.service.IMesProductInspectorService;

/**
 * 产品检验
Service业务层处理
 * 
 * @author yy
 * @date 2022-05-23
 */
@Service
public class MesProductInspectorServiceImpl implements IMesProductInspectorService 
{
    @Autowired
    private MesProductInspectorMapper mesProductInspectorMapper;

    /**
     * 查询产品检验
     * 
     * @param id 产品检验主键
     * @return 产品检验
     */
    @Override
    public MesProductInspectorVo selectMesProductInspectorById(Long id)
    {
        return mesProductInspectorMapper.selectMesProductInspectorById(id);
    }

    /**
     * 查询产品检验列表
     * 
     * @param mesProductInspectorVo 产品检验
     * @return 产品检验
     */
    @Override
    public List<MesProductInspectorVo> selectMesProductInspectorList(MesProductInspectorVo mesProductInspectorVo)
    {
        List<MesProductInspectorVo> list = mesProductInspectorMapper.selectMesProductInspectorList(mesProductInspectorVo);
        for (MesProductInspectorVo productInspector : list) {
            if (productInspector.getResult() == 1) {
                productInspector.setQualifiedRate(1D);
            } else {
                productInspector.setQualifiedRate(productInspector.getSampleQuantity() / productInspector.getQuantity());
            }
        }
        return list;
    }

    /**
     * 新增产品检验
     * 
     * @param mesProductInspector 产品检验
     * @return 结果
     */
    @Override
    public int insertMesProductInspector(MesProductInspector mesProductInspector)
    {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        mesProductInspector.setSerialNumber("CPJY" + format.format(date));
        mesProductInspector.setCheckUserId(SecurityUtils.getUserId());
        mesProductInspector.setInspectorTime(new Date());
        return mesProductInspectorMapper.insertMesProductInspector(mesProductInspector);
    }

    /**
     * 修改产品检验
     * 
     * @param mesProductInspector 产品检验
     * @return 结果
     */
    @Override
    public int updateMesProductInspector(MesProductInspector mesProductInspector)
    {
        return mesProductInspectorMapper.updateMesProductInspector(mesProductInspector);
    }

    /**
     * 批量删除产品检验
     * 
     * @param ids 需要删除的产品检验主键
     * @return 结果
     */
    @Override
    public int deleteMesProductInspectorByIds(Long[] ids)
    {
        return mesProductInspectorMapper.deleteMesProductInspectorByIds(ids);
    }

    /**
     * 删除产品检验信息
     * 
     * @param id 产品检验主键
     * @return 结果
     */
    @Override
    public int deleteMesProductInspectorById(Long id)
    {
        return mesProductInspectorMapper.deleteMesProductInspectorById(id);
    }

//    @Override
//    public List<MesProductInspectorVo> selectQualifiedRateReport(MesProductInspectorVo mesMaterialInspectorVo) {
//        List<MesProductInspectorVo> list = mesProductInspectorMapper.selectMesProductInspectorList(mesMaterialInspectorVo);
//        for (MesProductInspectorVo productInspector : list) {
//            if (productInspector.getResult() == 1) {
//                productInspector.setQualifiedRate(1D);
//            } else {
//                productInspector.setQualifiedRate(productInspector.getSampleQuantity() / productInspector.getQuantity());
//            }
//        }
//        return list;
//    }
}
